package com.web.boot.web.cms;

import com.alibaba.fastjson.JSONObject;
import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.PicPos;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.config.ErrorConfig;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.UploadFile;
import com.blue.http.message.View;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.web.cms.doc.facade.DocFileFacade;
import com.web.cms.doc.facade.WebFacade;
import com.web.cms.doc.message.DocFileRequest;
import com.web.cms.doc.message.DocFileResponse;
import com.web.core.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("cmsDocFileController")
@Http(url = "/cms")
public class DocFileController
{
	private static Logger logger = LoggerFactory.getLogger(DocFileController.class);
	
	@Autowired
	private DocFileFacade docFileFacade;
	@Autowired
	private WebFacade webFacade;
	@Autowired
	private StorageClient storageClient;

	private DictParser dictParser = DictParser.getInstance();
	private ErrorConfig errorConfig = ErrorConfig.getInstance();
	
	public DocFileController()
	{
	}
	
	@Http(url = "/doc_file_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		DocFileRequest docFileRequest = request.getQueryObject(DocFileRequest.class);
		return View.createBuilder().setView("/cms/doc_file_list")
				.put("model", docFileRequest)
				.put("map", dictParser.getStringMap())
				.put("webList", webFacade.listAll())
				.put("page", docFileFacade.listPage(docFileRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/doc_file_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		DocFileRequest docFileRequest = request.getPostObject(DocFileRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/cms/doc_file_list_grid")
				.put("page", docFileFacade.listPage(docFileRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/doc_file_upload_editor", method = HttpMethod.POST)
	public Object uploadEditor(HttpRequest request)
	//public void uploadEditor(@RequestParam("editormd-image-file") MultipartFile file, @RequestParam("webId") Integer webId, PrintWriter pw)
	{
		UploadFile file = request.getFile();
		int webId = request.getQueryInt("webId", 0);
		if (file.getFile() == null)
			return this.output(false, errorConfig.get(ErrorConfig.ERROR_UPLOAD), null);

		try
		{
			String key = storageClient.save(Bucket.CMS, file.getInputStream(), file.getOriginalName(), null);
			DocFileResponse response = this.addUpload(file, key, webId, PicPos.CONTENT);
			return this.output(true, "上传成功", response.getUrl());
		}
		catch (Exception e)
		{
			logger.error(errorConfig.get(ErrorConfig.ERROR_UPLOAD) + "：", e);
			return this.output(false, errorConfig.get(ErrorConfig.ERROR_UPLOAD), null);
		}
	}
	
	@Http(url = "/doc_file_upload", method = HttpMethod.POST)
	public Object upload(HttpRequest request)
	{
		UploadFile file = request.getFile();
		int webId = request.getPostInt("webId", 0);
		int pos = request.getPostInt("pos", 1);
		if (file == null)
			throw errorConfig.getException(ErrorConfig.ERROR_UPLOAD);

		try
		{
			String key = storageClient.save(Bucket.CMS, file.getInputStream(), file.getOriginalName(), null);
			DocFileResponse response = this.addUpload(file, key, webId, dictParser.getFromIndex(PicPos.class, pos));
			return response;
		}
		catch (Exception e)
		{
			logger.error(errorConfig.get(ErrorConfig.ERROR_UPLOAD) + "：", e);
			throw errorConfig.getException(ErrorConfig.ERROR_UPLOAD);
		}
		
	}
	
	private DocFileResponse addUpload(UploadFile file, String key, Integer webId, PicPos pos)
	{
		DocFileRequest request = new DocFileRequest();
		request.setWebId(webId);
		request.setName(file.getOriginalName());
		request.setKey(key);
		request.setSize((int)file.getSize());
		request.setPos(pos);
		request.setMimeType(file.getContentType());
		DocFileResponse response = docFileFacade.add(request);
		return response;
	}
	
	private Object output(boolean state, String message, String url)
	{
		JSONObject object = new JSONObject();
		object.put("success", state ? 1 : 0);
		object.put("message", message);
		object.put("url", url);
		return object;
	}
	
}
